
package com.zjff.myshop.admin.service.impl;

import com.zjff.myshop.front.pojo.vo.AddressVO;
import com.zjff.myshop.entity.Address;
import com.zjff.myshop.exception.BusinessException;
import com.zjff.myshop.mapper.AddressMapper;
import com.zjff.myshop.admin.service.AddressService;
import com.zjff.myshop.util.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

//保持容器中bean的名称不重复，对其中的一个bean进行自定义命名。
@Service("adminAddressServiceImpl")
public class AddressServiceImpl implements AddressService {

    @Resource
    private AddressMapper addressMapper;

    @Override
    @Transactional
    public Boolean insert(Address address) {
        Boolean isDefault = address.getIsDefault();
        if (isDefault != null && isDefault) {
            //添加默认地址，需要将原有的默认地址更改掉
            Address defaultAddress = addressMapper.selectDefaultByUserId(address.getUserId());
            if (defaultAddress != null) {
                defaultAddress.setIsDefault(false);
                int updateResult = addressMapper.update(defaultAddress);
                if (updateResult < 1) {
                    //原有的默认地址未更新成功，抛出异常
                    throw new BusinessException("数据库操作错误!");
                }
            }
        }
        return addressMapper.insert(address) > 0;
    }

    @Override
    public Boolean deleteById(Integer addressId) {
        return addressMapper.deleteByPrimaryKey(addressId) > 0;
    }

    @Override
    public Boolean update(Address address) {
        Address tempAddress = selectByPrimaryKey(address.getId());
        Boolean isDefault = address.getIsDefault();
        if (isDefault != null && isDefault) {
            //更改为默认地址，需要将原有的默认地址更改掉
            Address defaultAddress = addressMapper.selectDefaultByUserId(address.getUserId());
            if (defaultAddress != null && tempAddress != null && !defaultAddress.getId().equals(tempAddress.getId())) {
                //存在默认地址且默认地址并不是当前更改的地址
                defaultAddress.setIsDefault(false);
                int updateResult = addressMapper.update(defaultAddress);
                if (updateResult < 1) {
                    //原默认地址未更新成功，抛出异常
                    throw new BusinessException("数据库操作错误!");
                }
            }
        }
        return addressMapper.update(address) > 0;
    }

    @Override
    public Address selectByPrimaryKey(Integer addressId) {
        Address address = addressMapper.selectByPrimaryKey(addressId);
        if (address == null) {
            throw new BusinessException("数据不存在!");
        }
        return address;
    }

    @Override
    public List<AddressVO> selectListByUserId(Integer userId) {
        List<Address> addressList = addressMapper.selectListByUserId(userId);
        return BeanUtils.copyList(addressList, AddressVO.class);
    }

    @Override
    public Address selectDefaultByUserId(Integer userId) {
        return addressMapper.selectDefaultByUserId(userId);
    }
}
